﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KPIS.GERP.MVC.Classes;
using KPIS.GERP.SERVICE.BLL;
using KPIS.GERP.SERVICE.DAL;
using KPIS.GERP.SERVICE.Model;
using Telerik.Web.Mvc;

namespace KPIS.GERP.MVC.Controllers
{
    public class MessageController : Controller
    {
        BLLMessage bllMessage = new BLLMessage();

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult _AjaxBinding()
        {
            string strError;
            List<message> list_message = bllMessage.GetMessage(null, null, new SsMng().EmpSeq, null, null, null
                                                               , true, true, false, false, false, false, out strError).
                OrderByDescending(m => m.message_seq).ToList();

            return View(new GridModel<message>
            {
                Data = list_message
            });
        }


        public ActionResult ViewDetails(int message_seq)
        {
            message m = null;
            try
            {
                string strError;
                m = bllMessage.GetMessage(message_seq.ToString(), "", "", "", "", "", true, true, true,
                                          true, true, true,
                                          out strError)[0];
                bllMessage.MarkHasBeenRead(message_seq, new SsMng().EmpSeqI);
            }
            catch
            {

            }

            approval_task approvalTask = null;
            try
            {
                var tasks = new approval_task[0];
                using (var serviceClient = new ApprovalServiceWcfIisRef.AbsenceApprovalWCFClient())
                {
                    tasks = serviceClient.GetTasks();
                    approvalTask = tasks.FirstOrDefault(o => o.ApprovalTaskSeq == m.approval_task_seq);
                    ViewBag.ApprovalActionCountOfApprovalTask = approvalTask.ActionCount;
                }
            }
            catch
            {
            }

            try
            {
                if (approvalTask != null)
                    ViewBag.model_absence =
                        new Absence().GetHrmPsAbsence(approvalTask.Reference.ref_seq.ToString(), "", "", "",
                                                      true, true).FirstOrDefault();
            }
            catch (Exception ex)
            {

            }
            ViewBag.ApprovalTask = approvalTask;
            return View("ViewDetails", m);
        }

        [HttpPost]
        public ActionResult TakeAction(message model, int message_seq, string MessageTitle, string Reasons,
            string ActionStringHdf)
        {
            try
            {
                string strError;
                model = bllMessage.GetMessage(message_seq.ToString(), "", "", "", "", "", true, true, true,
                                              true, true, true, out strError)[0];
            }
            catch
            {
            }

            using (var serviceClient = new ApprovalServiceIisRef.ServiceClient())
            {
                if (model.approval_task_seq != null)
                {

                    approval_task approvalTasksFocus = null;
                    using (var serviceClientWcf = new ApprovalServiceWcfIisRef.AbsenceApprovalWCFClient())
                    {
                        approvalTasksFocus =
                            serviceClientWcf.GetTasks().FirstOrDefault(o => o.ApprovalTaskSeq == model.approval_task_seq);
                    }
                    serviceClient.ApproveAbsence(approvalTasksFocus.WfInstantId,
                                                 ApprovalUtil.TaskActionFromString(ActionStringHdf),
                                                 new SsMng().EmpSeqI, Reasons, MessageTitle);
                }
                else
                {
                    throw new Exception("รหัสงานอนุมัติไม่ถูกโหลดขึ้นมาอย่างถูกต้อง");
                }
            }

            return RedirectToAction("Index");
        }

        public ActionResult ViewAbsence(int RefSeq)
        {
            var absenceDal = new Absence();
            var model = absenceDal.GetHrmPsAbsence(RefSeq.ToString(), null, null, null, true, true).FirstOrDefault();
            return View(model);
        } 
         
    }
}
